package com.cjf.weather.weatherreport.controller;

import com.cjf.weather.weatherreport.service.WeatherReportService;
import com.cjf.weather.weatherreport.vo.City;
import com.cjf.weather.weatherreport.vo.WeatherResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cjf
 * @description 天气报道控制器
 * @create 2019-03-29 23:56
 */
@RestController
public class WeatherReportController {

    private static final String TITILE = "广东天气预报中心";

    @Autowired
    private WeatherReportService weatherReportService;

    private static final Logger logger = LoggerFactory.getLogger(WeatherReportController.class);

    /**
     * 根据城市id，进行天气预报展示
     *
     * @param cityId
     * @param model
     * @return
     */
    @GetMapping("/reportWeather/cityId/{cityId}")
    public ModelAndView reportWeatherByCityId(@PathVariable("cityId") String cityId, Model model) {
        model.addAttribute("titile", TITILE);
        model.addAttribute("cityId", cityId);
        WeatherResponse weatherResponse = weatherReportService.reportWeather(cityId);
        model.addAttribute("weatherReponse", weatherResponse);
        List<City> cityList = null;
        try {
            // TODO 改为由城市数据API微服务提供数据
            cityList = new ArrayList<>();
            City city = new City();
            city.setCityId("101280601");
            city.setCityName("深圳");
            cityList.add(city);

        } catch (Exception e) {
            logger.error("Exception!", e);
        }
        try {
            model.addAttribute("citylist", cityList);
        } catch (Exception e) {
            model.addAttribute("citylist", null);
            logger.error("获取城市信息出错" + e);
        }
        return new ModelAndView("/weather/report.html", "reportWeather", model);
    }

}
